package org.squabble.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.squabble.domain.Tag;

@Repository
public class TagDAOImpl extends AbstractHibernateDAO<Tag> implements TagDAO {
	private final Log log = LogFactory.getLog(this.getClass());

	@Autowired
	public TagDAOImpl(SessionFactory sessionFactory) {
		super(Tag.class, sessionFactory);
	}

	@Override
	public Tag getTag(String name) {
		log.debug("Searching tags for '" + name + "'");
		Tag tag = uniqueResult(criteria().add(Restrictions.ilike("name", name)));
		log.debug("Returning: " + tag);
		return tag;
	}

	@Override
	public Tag save(Tag tag) {
		Long id = (Long) currentSession().save(tag);
		tag = get(id);
		return tag;
	}
}
